import xlwt
import os
import numpy as np
def get_trans_Path(filepath):
    trans_path = False
    for dirpath, dirnames, filenames in os.walk(filepath):
        trans_path = dirnames
        #print(trans_path)
        break
        # print('dirpaath',dirpath)
        # print('dirnames')
        # print(dirnames)
    if trans_path:
        return trans_path
def getCoverage():
    count = 0  # 记录excel表格的行数
    wb = xlwt.Workbook()
    ws = wb.add_sheet('sheet')
    coverage_5_1000 = {}
    coverage_6_1000 = {}
    coverage_7_1000 = {}
    coverage_ajust_1000 = {}
    coverage_car_1000 = {}

    coverage_5_10000 = {}
    coverage_6_10000 = {}
    coverage_7_10000 = {}
    coverage_ajust_10000 = {}
    coverage_car_10000 = {}
    filepath = 'F:/paper_result/mr'
    paths1 = get_trans_Path(filepath)
    print(paths1)
    all_coverage_1000={}
    all_coverage_10000 = {}
    for i in paths1:
        coverage_1000 = {}
        coverage_10000 = {}
        for dirpath, dirnames, filenames in os.walk(filepath + '/' + i):
            for j in filenames:
                if 'output_coverage_1000.npy' in j:
                    dict = np.load(filepath + '/' + i + '/' + j, allow_pickle=True).item()
                    for k in dict.keys():
                        if k not in coverage_1000.keys():
                            coverage_1000[k] = 1
                        if k not in all_coverage_1000.keys():
                            all_coverage_1000[k] = 1
                    if '5' in j:
                        for k in dict.keys():
                            if k not in coverage_5_1000.keys():
                                coverage_5_1000[k] = 1
                    if '6' in j:
                        for k in dict.keys():
                            if k not in coverage_6_1000.keys():
                                coverage_6_1000[k] = 1
                    if '7' in j:
                        for k in dict.keys():
                            if k not in coverage_7_1000.keys():
                                coverage_7_1000[k] = 1
                    if 'ajust' in j:
                        for k in dict.keys():
                            if k not in coverage_ajust_1000.keys():
                                coverage_ajust_1000[k] = 1
                    if 'car' in j:
                        for k in dict.keys():
                            if k not in coverage_car_1000.keys():
                                coverage_car_1000[k] = 1
                if 'output_coverage_10000.npy' in j:
                    dict = np.load(filepath + '/' + i + '/' + j, allow_pickle=True).item()
                    for k in dict.keys():
                        if k not in coverage_10000.keys():
                            coverage_10000[k] = 1
                        if k not in all_coverage_10000.keys():
                            all_coverage_10000[k] = 1
                    if '5' in j:
                        for k in dict.keys():
                            if k not in coverage_5_10000.keys():
                                coverage_5_10000[k] = 1
                    if '6' in j:
                        for k in dict.keys():
                            if k not in coverage_6_10000.keys():
                                coverage_6_10000[k] = 1
                    if '7' in j:
                        for k in dict.keys():
                            if k not in coverage_7_10000.keys():
                                coverage_7_10000[k] = 1
                    if 'ajust' in j:
                        for k in dict.keys():
                            if k not in coverage_ajust_10000.keys():
                                coverage_ajust_10000[k] = 1
                    if 'car' in j:
                        for k in dict.keys():
                            if k not in coverage_car_10000.keys():
                                coverage_car_10000[k] = 1
        ws.write(count,0,i+'1000')
        ws.write(count, 1, sum(coverage_1000.values())/1000)
        ws.write(count,2,i+'10000')
        ws.write(count, 3, sum(coverage_10000.values())/10000)
        count = count + 1
    ws.write(count, 0, '5_1000')
    ws.write(count, 1, sum(coverage_5_1000.values())/1000)
    ws.write(count, 2, '5_10000')
    ws.write(count, 3, sum(coverage_5_10000.values())/10000)
    count = count + 1
    ws.write(count, 0, '6_1000')
    ws.write(count, 1, sum(coverage_6_1000.values())/1000)
    ws.write(count, 2, '6_10000')
    ws.write(count, 3, sum(coverage_6_10000.values())/10000)
    count = count + 1
    ws.write(count, 0, '7_1000')
    ws.write(count, 1, sum(coverage_7_1000.values())/1000)
    ws.write(count, 2, '7_10000')
    ws.write(count, 3, sum(coverage_7_10000.values())/10000)
    count = count + 1
    ws.write(count, 0, 'ajust_1000')
    ws.write(count, 1, sum(coverage_ajust_1000.values())/1000)
    ws.write(count, 2, 'ajust_10000')
    ws.write(count, 3, sum(coverage_ajust_10000.values())/10000)
    count = count + 1
    ws.write(count, 0, 'car_1000')
    ws.write(count, 1, sum(coverage_car_1000.values())/1000)
    ws.write(count, 2, 'car_10000')
    ws.write(count, 3, sum(coverage_car_10000.values())/10000)
    count = count + 1
    ws.write(count, 0, 'mr_all_1000')
    ws.write(count, 1, sum(all_coverage_1000.values())/1000)
    ws.write(count, 2, 'mr_all_10000')
    ws.write(count, 3, sum(all_coverage_10000.values())/10000)
    count = count + 1
    deeptest_coverage_1000 = {}
    deeptest_coverage_10000 = {}
    for dirpath, dirnames, filenames in os.walk('F:/paper_result/deeptest'):
        for j in filenames:
            if 'output_coverage_1000.npy' in j:
                dict = np.load('F:/paper_result/deeptest' + '/' + j, allow_pickle=True).item()
                for k in dict.keys():
                    if k not in deeptest_coverage_1000.keys():
                        deeptest_coverage_1000[k] = 1
                    if k not in coverage_1000.keys():
                        all_coverage_1000[k] = 1
                    if '5' in j:
                        for k in dict.keys():
                            if k not in coverage_5_1000.keys():
                                coverage_5_1000[k] = 1
                    if '6' in j:
                        for k in dict.keys():
                            if k not in coverage_6_1000.keys():
                                coverage_6_1000[k] = 1
                    if '7' in j:
                        for k in dict.keys():
                            if k not in coverage_7_1000.keys():
                                coverage_7_1000[k] = 1
                    if 'ajust' in j:
                        for k in dict.keys():
                            if k not in coverage_ajust_1000.keys():
                                coverage_ajust_1000[k] = 1
                    if 'car' in j:
                        for k in dict.keys():
                            if k not in coverage_car_1000.keys():
                                coverage_car_1000[k] = 1
            if 'output_coverage_10000.npy' in j:
                dict = np.load('F:/paper_result/deeptest' + '/' + j, allow_pickle=True).item()
                for k in dict.keys():
                    if k not in deeptest_coverage_10000.keys():
                        deeptest_coverage_10000[k] = 1
                    if k not in coverage_10000.keys():
                        all_coverage_10000[k] = 1
                    if '5' in j:
                        for k in dict.keys():
                            if k not in coverage_5_10000.keys():
                                coverage_5_10000[k] = 1
                    if '6' in j:
                        for k in dict.keys():
                            if k not in coverage_6_10000.keys():
                                coverage_6_10000[k] = 1
                    if '7' in j:
                        for k in dict.keys():
                            if k not in coverage_7_10000.keys():
                                coverage_7_10000[k] = 1
                    if 'ajust' in j:
                        for k in dict.keys():
                            if k not in coverage_ajust_10000.keys():
                                coverage_ajust_10000[k] = 1
                    if 'car' in j:
                        for k in dict.keys():
                            if k not in coverage_car_10000.keys():
                                coverage_car_10000[k] = 1
    ws.write(count, 0, 'deeptest_all_1000')
    ws.write(count, 1, sum(deeptest_coverage_1000.values()) / 1000)
    ws.write(count, 2, 'deeptest_all_10000')
    ws.write(count, 3, sum(deeptest_coverage_10000.values()) / 10000)
    count = count + 1
    ws.write(count, 0, 'all_1000')
    ws.write(count, 1, sum(all_coverage_1000.values()) / 1000)
    ws.write(count, 2, 'all_10000')
    ws.write(count, 3, sum(all_coverage_10000.values()) / 10000)
    count = count + 1
    ws.write(count, 0, '5_1000')
    ws.write(count, 1, sum(coverage_5_1000.values()) / 1000)
    ws.write(count, 2, '5_10000')
    ws.write(count, 3, sum(coverage_5_10000.values()) / 10000)
    count = count + 1
    ws.write(count, 0, '6_1000')
    ws.write(count, 1, sum(coverage_6_1000.values()) / 1000)
    ws.write(count, 2, '6_10000')
    ws.write(count, 3, sum(coverage_6_10000.values()) / 10000)
    count = count + 1
    ws.write(count, 0, '7_1000')
    ws.write(count, 1, sum(coverage_7_1000.values()) / 1000)
    ws.write(count, 2, '7_10000')
    ws.write(count, 3, sum(coverage_7_10000.values()) / 10000)
    count = count + 1
    ws.write(count, 0, 'ajust_1000')
    ws.write(count, 1, sum(coverage_ajust_1000.values()) / 1000)
    ws.write(count, 2, 'ajust_10000')
    ws.write(count, 3, sum(coverage_ajust_10000.values()) / 10000)
    count = count + 1
    ws.write(count, 0, 'car_1000')
    ws.write(count, 1, sum(coverage_car_1000.values()) / 1000)
    ws.write(count, 2, 'car_10000')
    ws.write(count, 3, sum(coverage_car_10000.values()) / 10000)
    count = count + 1
    ws.write(count, 0, 'mr_all_1000')
    ws.write(count, 1, sum(all_coverage_1000.values()) / 1000)
    ws.write(count, 2, 'mr_all_10000')
    ws.write(count, 3, sum(all_coverage_10000.values()) / 10000)
    count = count + 1
    for dirpath, dirnames, filenames in os.walk('F:/paper_result/deeptest_class'):
        for j in filenames:
            if 'output_coverage_1000_' in j:
                dict = np.load('F:/paper_result/deeptest_class' + '/' + j, allow_pickle=True).item()
                print(dict)
                if 'blur' in j:
                    ws.write(count, 0, 'blur_1000')
                    ws.write(count, 1, sum(dict.values()) / 1000)
                    count = count + 1
                if 'brightness' in j:
                    ws.write(count, 0, 'brightness_1000')
                    ws.write(count, 1, sum(dict.values()) / 1000)
                    count = count + 1
                if 'contrast' in j:
                    ws.write(count, 0, 'contrast_1000')
                    ws.write(count, 1, sum(dict.values()) / 1000)
                    count = count + 1
                if 'rotation' in j:
                    ws.write(count, 0, 'rotation_1000')
                    ws.write(count, 1, sum(dict.values()) / 1000)
                    count = count + 1
                if 'scale' in j:
                    ws.write(count, 0, 'scale_1000')
                    ws.write(count, 1, sum(dict.values()) / 1000)
                    count = count + 1
                if 'shear' in j:
                    ws.write(count, 0, 'shear_1000')
                    ws.write(count, 1, sum(dict.values()) / 1000)
                    count = count + 1
                if 'translation' in j:
                    ws.write(count, 0, 'translation_1000')
                    ws.write(count, 1, sum(dict.values()) / 1000)
                    count = count + 1
            if 'output_coverage_10000_' in j:
                dict = np.load('F:/paper_result/deeptest_class' + '/' + j, allow_pickle=True).item()
                if 'blur' in j:
                    ws.write(count, 0, 'blur_10000')
                    ws.write(count, 1, sum(dict.values()) / 10000)
                    count = count + 1
                if 'brightness' in j:
                    ws.write(count, 0, 'brightness_10000')
                    ws.write(count, 1, sum(dict.values()) / 10000)
                    count = count + 1
                if 'contrast' in j:
                    ws.write(count, 0, 'contrast_10000')
                    ws.write(count, 1, sum(dict.values()) / 10000)
                    count = count + 1
                if 'rotation' in j:
                    ws.write(count, 0, 'rotation_10000')
                    ws.write(count, 1, sum(dict.values()) / 10000)
                    count = count + 1
                if 'scale' in j:
                    ws.write(count, 0, 'scale_10000')
                    ws.write(count, 1, sum(dict.values()) / 10000)
                    count = count + 1
                if 'shear' in j:
                    ws.write(count, 0, 'shear_10000')
                    ws.write(count, 1, sum(dict.values()) / 10000)
                    count = count + 1
                if 'translation' in j:
                    ws.write(count, 0, 'translation_10000')
                    ws.write(count, 1, sum(dict.values()) / 10000)
                    count = count + 1
    wb.save('F:/paper_result/output_coverage.xls')
if __name__ == '__main__':
    getCoverage()